#include <iostream>
using namespace std;
/*
24、
Create a struct that holds an int and a pointer to another instance of the same struct.
Write a function that takes the address of one of these structs and an int indicating the length of the list you want created.
This function will make a whole chain of these structs (a linked list),
starting from the argument (the head of the list), with each one pointing to the next.
Make the new structs using new, and put the count (which object number this is) in the int.
In the last struct in the list, put a zero value in the pointer to indicate that it’s the end.
Write a second function that takes the head of your list and moves through to the end,
printing out both the pointer value and the int value for each one.

25、
Repeat Exercise 24,
but put the functions inside a struct instead of using “raw” structs and functions.
*/

struct node
{
    int data;
    struct node *next;
    void initLink(node *start, int len);
    void ErgodicLink(node *start);
};


int main()
{
    node link;
    node *start = new node;
    link.initLink(start, 8);
    link.ErgodicLink(start);
    return 0;
}

void node::initLink(node *start, int len)
{
    node *q = start;
    node *p;
    for (int i = 0; i < len; i++)
    {
        p = (node *)malloc(sizeof(struct node));
        p->data = i + 1;
        q->next = p;
        q = q->next;
        p = p->next;
    }
    q->next = NULL;
};

void node::ErgodicLink(node *start)
{
    node *p = start;
    while (true)
    {
        if (p == NULL)
        {
            break;
        }
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}
